/*
Copyright (c) 2009 Hayden Curran <haydenz@gmail.com>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
    * The names of its contributors may not be used to endorse or promote
        products derived from this software without specific prior
        written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "irc.h"
#include "rcon.h"
#include "server_log.h"

int main(void)
{
    rcon_status_t rcon;
    buffer_t irc_buffer;
    pug_t pug;
    int ircfd, slfd, ret, i;
    fd_set read_set;
    fd_set master_set;
    int fdmax; // Highest socket descriptor we have

    #ifdef WIN32
    WSADATA wsaData;

    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
        perror("winsock init");
        exit(1);
    }
    #endif
    FD_ZERO(&master_set);
    fdmax = 0;

    irc_init(&irc_buffer, &pug);
    rcon_init(&rcon);

    ircfd = irc_connect();
    rcon.rconfd = rcon_connect();
    slfd = server_log_bind();

    if (rcon.rconfd == -1) {
        exit(1);
    }

    FD_SET(ircfd, &master_set);
    FD_SET(rcon.rconfd, &master_set);
    FD_SET(slfd, &master_set);
    fdmax = ircfd;  // ircfd is our highest socket descriptor
    if (rcon.rconfd > fdmax) fdmax = rcon.rconfd; // now rconfd is our highest
    if (slfd > fdmax) fdmax = slfd; // now slfd is our highest

    rcon_authenticate(&rcon);

    while (1) {
        read_set = master_set;
        ret = select(fdmax+1, &read_set, NULL, NULL, NULL);
        if (ret == -1) {
            perror("select");
            exit(1);
        }

        for (i = 0; i <= fdmax; i++) {
            if (FD_ISSET(i, &read_set)) {
                if (i == ircfd) {
                    ret = irc_receive(i, &irc_buffer, &pug);
                    if (ret <= 0) FD_CLR(i, &master_set);
                }
                else if (i == rcon.rconfd) {
                    ret = rcon_receive(&rcon);
                    if (ret <= 0) {
                        FD_CLR(i, &master_set);
                    }
                }
                else if (i == slfd) {
                    ret = server_log_receive(i, ircfd, &pug);
                    if (ret <= 0) FD_CLR(i, &master_set);
                }
            }
        }
    }

    #ifdef WIN32
    WSACleanup();
    #endif

    return 0;
}











